package pers.cj.pdos.controller;


import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.RequestLogoEntity;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.service.IUserService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.FileProperUtil;
import pers.cj.pdos.utils.ImgUtil;
import pers.cj.pdos.vo.CompanyVo;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Base64;
import java.util.Date;

/**
 * <p>
 * 企业账号信息 前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/account")
public class CompanyController extends BaseController {
    @Autowired
    private IUserService userService;


    @RequestMapping("/company")
    @RequiresPermissions("company:insert")
    public String company(){
        return "company/company";
    }

    @RequestMapping("/companyInfo")
    @RequiresPermissions("company:insert")
    public String companyInfo(){
        return "company/companyInfo";
    }

    @ResponseBody
    @RequestMapping("/getUserCompany")
    public ResponseData getUserCompany(){

        Company company = getCompany();
        CompanyVo companyVo = DataUtil.genVoFromEntity(CompanyVo.class, company);
        companyVo.setUserNumber(userService.count(company.getId(),null,null,1));
        return ResponseData.success(companyVo);
    }

    @RequestMapping("/companyLogo")
    @RequiresPermissions("company:insert")
    public String companyLogo(){
        return "company/companyLogo";
    }

    @ResponseBody
    @RequiresPermissions("company:insert")
    @RequestMapping("update")
    public ResponseEntity update(@RequestBody Company company) {
        company.setTime(new Date());
        companyService.update(company,getSnIndustry().getId());
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("company:insert")
    @RequestMapping("updateNotice")
    public ResponseEntity updateNotice(@RequestBody Company temp) {
        Company company = getCompany();
        temp.setId(company.getId());
        companyService.update(temp);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/getNotice")
    public ResponseData getNotice(){
        Company company = getCompany();
        return ResponseData.success(company.getNotice());
    }

    @ResponseBody
    @RequiresPermissions("company:insert")
    @RequestMapping("/uploadLogo")
    public ResponseEntity uploadLogo(@RequestBody RequestLogoEntity requestLogoEntity, HttpServletRequest request) throws IOException {

        Company company = getCompany();

        String characterEncoding = request.getCharacterEncoding();
        String base64Img = requestLogoEntity.getBase64();

        //image/png;base64,
        String[] fileData = URLDecoder.decode(base64Img,characterEncoding).split(";base64,");

        String type = fileData[0].split("/")[1];
        Logger.getLogger(this.getClass()).info(type);

        String data = fileData[1];
        Logger.getLogger(this.getClass()).info( "len="+ data.length() + "," + data);


        //将传过来的数据解码
        byte[] imageData = Base64.getDecoder().decode(data);


        //保存头像的路径
        String headPath = FileProperUtil.getInstance().getVal("image.company");
        String filename = ImgUtil.getFileName(company.getAccount(), type);

        try {
            FileOutputStream out = new FileOutputStream(headPath + filename); // 输出文件路径

            System.out.println(headPath + filename);
            out.write(imageData);
            out.close();
        } catch (Exception e) {
            return ResponseEntity.fail("保存图片失败");
        }

        company.setLogo(filename);
        companyService.update(company);
        return ResponseEntity.success();
    }
}
